package com.fh.fruit.controller.user;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.Role;
import com.fh.fruit.service.user.UserManager;
import com.fh.service.system.menu.MenuManager;
import com.fh.service.system.role.RoleManager;
import com.fh.util.AppUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;

@Controller
@RequestMapping(value="/fruitUser")
public class FarmUserController extends BaseController {

	String menuUrl = "fruitUser/listFruitUsers"; //菜单地址(权限用)
	
	@Resource(name="fruitUserService")
	private UserManager fruitUserService;
	@Resource(name="roleService")
	private RoleManager roleService;
	@Resource(name="menuService")
	private MenuManager menuService;
	
	/**显示会员列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listFruitUsers")
	public ModelAndView listUsers(Page page)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String petName = pd.getString("petName");
		String phone = pd.getString("phone");//关键词检索条件
		if(null != petName && !"".equals(petName)){
			pd.put("petName", petName.trim());
		}
		if(null != phone && !"".equals(phone)){
			pd.put("phone", phone.trim());
		}
		page.setPd(pd);
		List<PageData>	fruitUserList = fruitUserService.listfruitUsers(page);	//列出用户列表
		mv.setViewName("farm/user/User_list");
		mv.addObject("fruitUserList", fruitUserList);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	/**去新增用户页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goAddUser")
	public ModelAndView goAddUser()throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("system/listfruitUsers/listfruitUsers_add");
		return mv;
	}
	
	/**新增用户
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveUser")
	public ModelAndView saveU(HttpServletRequest requset) throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		logBefore(logger, Jurisdiction.getUsername()+"新增用户");
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData(); 
		fruitUserService.saveUser(pd);
		mv.setViewName("save_result");
		return mv;
	}
	
	
	/**删除用户
	 * @param out
	 * @throws Exception 
	*/
	@RequestMapping(value="/deleteU")
	public void deleteU(PrintWriter out) throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;} //校验权限
		logBefore(logger, Jurisdiction.getUsername()+"删除会员");
		PageData pd = new PageData();
		pd = this.getPageData();
		fruitUserService.updateUserState(pd);
		out.write("success");
		out.close();
	}
	/**批量删除
	 * @return
	*/
	@RequestMapping(value="/deleteAllU")
	@ResponseBody
	public Object deleteAllU() {
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){} //校验权限
		logBefore(logger, Jurisdiction.getUsername()+"批量删除会员");
		PageData pd = new PageData();
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String USER_ID = pd.getString("USER_IDS");
			if(null != USER_ID && !"".equals(USER_ID)){
				String ArrayUSER_IDS[] = USER_ID.split(",");
				for (int i = 0; i < ArrayUSER_IDS.length; i++) {
					String str = ArrayUSER_IDS[i];
					pd.put("ID", str);
					pd.put("state", "02");
					fruitUserService.updateUserState(pd);
				}
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
}
